<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="规则" prop="ruleName">
        <el-input v-model="queryParams.ruleName" placeholder="请输入规则名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推荐人" prop="referrerName">
        <el-input
          v-model="queryParams.referrerName"
          placeholder="请输入推荐人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="卡订单编号" align="center" prop="id" />
      <el-table-column label="所属区域" align="center" prop="regionName" />
      <el-table-column label="所属门店" align="center" prop="storeName" /> 
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="客户电话" align="center" prop="phoneNumber" />
      <el-table-column label="卡编号" align="center" prop="cardId" />
      <el-table-column label="卡名称" align="center" prop="ruleName" />
      <el-table-column label="推荐技师" align="center" prop="referrerName" />
      <el-table-column label="提成金额" align="center" prop="royalty" />
      <el-table-column label="会员卡价格" align="center" prop="originPrice" />
      <el-table-column label="原有卡抵扣金额" align="center" prop="remainPrincipal" /> 
      <el-table-column label="实付金额" align="center" prop="actuallyMoney" />

      <el-table-column label="优惠券名称" align="center" prop="couponNames" />
      <el-table-column label="优惠券数量" align="center" prop="couponCounts" />
      
      <el-table-column label="类型" align="center"  >
        <template slot-scope="scope">
          <span >  {{ scope.row.remainPrincipal==null?'新卡':'续费' }}</span>
        </template>
     </el-table-column>
    <el-table-column label="来源" align="center"  >
        <template slot-scope="scope">
          <span  v-if="scope.row.orderSource==1">小程序</span>
          <span  v-if="scope.row.orderSource==2">管理端</span>
        </template>
     </el-table-column>

      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="{ row }">
          {{ row.payType | getPayType }}
        </template>
      </el-table-column>


      <el-table-column label="支付交易号" align="center" prop="payType">
        <template slot-scope="scope">
          <span>{{scope.row.transactionId!=null?scope.row.transactionId: scope.row.id}}</span>
        </template>
      </el-table-column>


      <el-table-column label="购卡时间" align="center" prop="createTime" />
      <!-- <el-table-column label="是否支付" align="center" prop="isPayment">
        <template slot-scope="{row}">
          {{ row.isPayment == 2 ? '已支付' : '未支付'}}
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员卡用户购买订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="续费卡id" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入续费卡id" />
        </el-form-item>
        <el-form-item label="规则id" prop="ruleId">
          <el-input v-model="form.ruleId" placeholder="请输入规则id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="推荐人id" prop="referrerId">
          <el-input v-model="form.referrerId" placeholder="请输入推荐人id" />
        </el-form-item>
        <el-form-item label="是否支付" prop="isPayment">
          <el-input v-model="form.isPayment" placeholder="请输入是否支付" />
        </el-form-item>
        <el-form-item label="商户订单号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入商户订单号" />
        </el-form-item>
        <el-form-item label="微信支付订单号" prop="transactionId">
          <el-input
            v-model="form.transactionId"
            placeholder="请输入微信支付订单号"
          />
        </el-form-item>
        <el-form-item label="商户退款单号" prop="outRefundNo">
          <el-input
            v-model="form.outRefundNo"
            placeholder="请输入商户退款单号"
          />
        </el-form-item>
        <el-form-item label="实付金额" prop="actuallyMoney">
          <el-input v-model="form.actuallyMoney" placeholder="请输入实付金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/system/member_order";
export default {
  filters: {
    getPayType: (type) => {
      let str = "";
      switch (type - 0) {
        case 1:
          str = "微信";
          break;
        case 2:
          str = "支付宝";
          break;
        case 3:
          str = "现金";
          break;
        case 4:
          str = "银行卡";
          break;
        case 5:
          str = "店铺储蓄卡";
          break;
        case 6:
          str = "其他";
          break;
        case 7:
          str = "扫码支付";
          break;
        case 8:
          str = "团购劵";
          break;
      }
      return str;
    },
  },
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员卡用户购买订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardId: null,
        ruleName: null,
        userName: null,
        referrerName: null,
        isPayment: null,
        outTradeNo: null,
        transactionId: null,
        outRefundNo: null,
        actuallyMoney: null,
        payType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员卡用户购买订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cardId: null,
        ruleId: null,
        userId: null,
        referrerId: null,
        isPayment: null,
        createTime: null,
        updateTime: null,
        outTradeNo: null,
        transactionId: null,
        outRefundNo: null,
        actuallyMoney: null,
        payType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员卡用户购买订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员卡用户购买订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除会员卡用户购买订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/member/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
